﻿using ResultCollector;

internal static class Program
{
    private static async Task Main()
    {
        Console.Write("Simulation Base Directory Path: ");
        var simulation = Console.ReadLine();

        DirectoryInfo baseDir = new DirectoryInfo(simulation!);
        var settings = new DiceSettings(baseDir.FullName);
        var executable = new SimulationExecutable(baseDir.FullName);

        Console.Write("Starting Ratio: ");
        var starting = float.Parse(Console.ReadLine()!);
        Console.Write("Ending Ratio: ");
        var ending = float.Parse(Console.ReadLine()!);
        Console.Write("Ratio Step: ");
        var step = float.Parse(Console.ReadLine()!);

        Console.WriteLine();
        await Simulate(executable, settings, FloatRange(starting, ending, step));

        Console.WriteLine("Completed. Press any key to exit.");
        _ = Console.ReadKey();
    }

    private static IEnumerable<float> FloatRange(float starting, float ending, float step)
    {
        for (var current = starting; current < ending; current += step)
            yield return current;
    }

    private static async Task Simulate(
        SimulationExecutable executable, DiceSettings settings,
        IEnumerable<float> ratios)
    {
        Console.WriteLine($"Preparing...");

        var tenSeconds = new TimeSpan(0, 0, 10);
        var twoMinutes = new TimeSpan(0, 2, 0);
        FileInfo tempOutput = new FileInfo("results/temp.txt");
        tempOutput.Directory?.Create();
        using StreamWriter output = new StreamWriter("results/results.txt", false);
        output.WriteLine("ratio,side,headOrTail,neither");
        output.Flush();

        settings.OutputFile = tempOutput.FullName;
        settings.Scale = 0.3F;
        settings.Count = 100;

        foreach (var ratio in ratios)
        {
            Console.WriteLine($"Preparing For {ratio}...");

            settings.DiameterToHeight = ratio;
            settings.RegionStarting = new Vector3(-200F, ratio * 1.2F, -200F);
            settings.RegionEnding = new Vector3(200F, ratio * 1.2F, 200F);
            settings.RecordingTime = 10F;
            settings.LastingTime = 11F;

            settings.Write();

            Console.WriteLine($"Starting Simulation For {ratio}...");
            tempOutput.Delete();
            _ = executable.Start();
            await Task.Delay(twoMinutes);
            executable.Stop();
            Console.WriteLine($"Simulation Ended For {ratio}.");

            await Task.Delay(tenSeconds);

            Console.WriteLine($"Collecting Result For {ratio}...");
            using (var reader = new StreamReader(tempOutput.FullName))
            {
                _ = reader.ReadLine();
                _ = reader.ReadLine();
                for (; ; )
                {
                    var line = reader.ReadLine();
                    if (string.IsNullOrEmpty(line))
                        break;
                    output.WriteLine($"{ratio},{line}");
                }
                output.Flush();
            }
            Console.WriteLine($"Result Collected For {ratio}.");

            await Task.Delay(tenSeconds);
        }
    }
}

